package com.hjc.service;

import com.hjc.dao.Impl.UserDaoImpl;
import com.hjc.dao.user.UserDao;
import com.hjc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户业务类，实现对User的业务管理
 */
@Service("userService")
public class UserServiceImpl  implements  UserService{

//为dao属性注入名为userDao的Bean
 //   @Resource(name="userDao")
    //实例化所依赖的UserDao对象
//    private UserDao dao = new UserDaoImpl();
  //  private UserDao dao = UserDaoFactory.getInstance();

//    public void setDao(UserDao dao) {
//        this.dao = dao;
//    }
    //声明接口类型的引用和具体实现类解耦合



    private  UserDao dao ;
    @Resource
    public  void setUserDao(UserDao userDao){
        this.dao = userDao;
    }
    //无参构造
    public UserServiceImpl(UserDaoImpl dao){

        this.dao = dao;
    }
    //用于为dao属性赋值的构造方法
    @Autowired
    public UserServiceImpl(@Qualifier("userDao") UserDao dao){

        this.dao = dao;
    }

    public UserServiceImpl() {

    }

    public void addNewUser(User user) {
        //调用用户DAO的方法保存用户信息
        dao.save(user);
    }
    @Autowired
    public void setDao(@Qualifier("userDao") UserDaoImpl dao) {
        this.dao = dao;
    }


}
